package common;

import java.io.Serializable;
import java.util.LinkedList;

//One instance per player of this object, to keep track of player positions etc.
public class PlayerGameBoard implements Serializable {
	private static final long serialVersionUID = 1L;
	private static final int DISABLED = 0;
	private static final int CURRENT = 1;
	private static final int AVAILABLE = 2;
	private final LinkedList<Integer> stateList = new LinkedList<Integer>();
	private final LinkedList<Tile> availableTileList = new LinkedList<Tile>();
	private Tile currentTilePos;

	public PlayerGameBoard() {
		// super();
		initialSetup();
	}

	private void initialSetup() {

		for (int i = 0; i < 49; i++) {
			this.stateList.add(i, DISABLED);
		}
		this.stateList.set(0, CURRENT); //

	}

	public void addAvailableTile(final Tile tile) {
		this.availableTileList.add(tile);
	}

	public void diceRolled(final int rolledNumber,
			final LinkedList<Tile> tileList) {
		new Step(this, rolledNumber, this.currentTilePos, tileList);
		setState(AVAILABLE);
	}

	public void tileChoosen(final Tile choosenTile) {
		setState(DISABLED);
		setGameBoardState(this.currentTilePos.position(), DISABLED);
		setGameBoardState(choosenTile.position(), CURRENT);
		this.currentTilePos = choosenTile;
		this.availableTileList.clear();
	}

	private void setState(final int STATE) {
		for (int i = 0; i < this.availableTileList.size(); i++) {
			final int av = this.availableTileList.get(i).position();
			setGameBoardState(av, STATE);
		}
	}

	/*
	 * private void setAllStates(final int STATE) { for (int i = 0; i <
	 * this.stateList.size(); i++) { final int av = this.stateList.get(i);
	 * setGameBoardState(av, STATE); } }
	 */

	private void setGameBoardState(final int index, final int state) {
		this.stateList.set(index, state);
	}

	public LinkedList<Integer> getStateList() {
		return this.stateList;
	}

	public Tile getCurrentTile() {
		return this.currentTilePos;
	}

	public void setCurrentTile(final Tile tile, final int STATE) {
		this.currentTilePos = tile;
		this.stateList.set(tile.position(), STATE);
		System.out.println("currenttile: " + this.currentTilePos.position()
				+ "");
		System.out.println("tile to be ported to: " + tile.position());
		System.out.println("statelist: " + this.stateList.get(tile.position()));
	}
}
